package com.selune.online.ordering.system.project.tenant.service;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.selune.online.ordering.system.project.api.base.constant.enums.OrderStatusEnum;
import com.selune.online.ordering.system.project.api.base.entity.OrderDetail;
import com.selune.online.ordering.system.project.api.base.entity.OrderOverview;
import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.api.mapper.GoodsInfoMapper;
import com.selune.online.ordering.system.project.api.mapper.OrderDetailMapper;
import com.selune.online.ordering.system.project.api.mapper.OrderOverviewMapper;
import com.selune.online.ordering.system.project.api.pojo.order.dto.CartGoodsDTO;
import com.selune.online.ordering.system.project.api.pojo.order.dto.OrderSearchDTO;
import com.selune.online.ordering.system.project.api.pojo.order.vo.OrderOverviewVO;
import com.selune.online.ordering.system.project.api.service.tenant.ITenantOrderService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @author xiaoyp
 * @date 2020/4/3
 * <p>
 * Description:
 */
@Service
@Slf4j
public class TenantOrderServiceImpl implements ITenantOrderService {
	
	@Resource
	private OrderOverviewMapper orderOverviewMapper;
	@Resource
	private OrderDetailMapper orderDetailMapper;
	@Resource
	private GoodsInfoMapper goodsInfoMapper;
	
	@Override
	public Response<Boolean> finishOrder(Long orderId) {
		if (null == orderId) {
			log.error("ACCESS_LOG[]TenantOrderServiceImpl[]finishOrder[]order id is null!");
			return Response.error("订单id为空");
		}
		OrderOverview orderOverview = orderOverviewMapper.selectById(orderId);
		if (ObjectUtils.isEmpty(orderOverview)) {
			log.error("ACCESS_LOG[]TenantOrderServiceImpl[]finishOrder[]order is exist!");
			return Response.error("订单不存在");
		}
		
		int result = orderOverviewMapper.update(
			null,
			Wrappers.<OrderOverview>lambdaUpdate()
				.eq(OrderOverview::getOrderId, orderId)
				.set(OrderOverview::getOrderStatus, OrderStatusEnum.FINISH_ORDER.code())
				.set(OrderOverview::getFinishTime, new Date())
		);
		
		if (result == 1) {
			return Response.success("订单完成", Boolean.TRUE);
		} else {
			return Response.error("完成订单失败");
		}
	}
	
	@Override
	public Response<Page<OrderOverviewVO>> listOrder(Integer current, Integer size, OrderSearchDTO orderSearchDTO) {
		
		int newCurrent = null == current ? 1 : current;
		int pageSize = null == size ? 10 : size;
		Page<OrderOverview> page = new Page<>(newCurrent, pageSize);
		Page<OrderOverview> orderOverviewPage = orderOverviewMapper.selectPage(page,
			Wrappers.<OrderOverview>lambdaQuery()
				.eq(ObjectUtils.isNotEmpty(orderSearchDTO.getUserId()), OrderOverview::getUserId,
					orderSearchDTO.getUserId())
				.eq(ObjectUtils.isNotEmpty(orderSearchDTO.getOrderPersonName()), OrderOverview::getOrderPersonName,
					orderSearchDTO.getOrderPersonName())
				.eq(ObjectUtils.isNotEmpty(orderSearchDTO.getOrderPersonPhone()), OrderOverview::getOrderPersonPhone,
					orderSearchDTO.getOrderPersonPhone())
				.eq(ObjectUtils.isNotEmpty(orderSearchDTO.getOrderAddress()), OrderOverview::getOrderAddress,
					orderSearchDTO.getOrderAddress())
				.eq(ObjectUtils.isNotEmpty(orderSearchDTO.getOrderStatus()), OrderOverview::getOrderStatus,
					orderSearchDTO.getOrderStatus())
				.eq(ObjectUtils.isNotEmpty(orderSearchDTO.getPayStatus()), OrderOverview::getPayStatus,
					orderSearchDTO.getPayStatus())
				.orderByDesc(OrderOverview::getUpdateTime)
		);
		
		Page<OrderOverviewVO> orderOverviewVoPage = new Page<>();
		BeanUtils.copyProperties(orderOverviewPage, orderOverviewVoPage);
		List<OrderOverviewVO> orderOverviewVOList = new ArrayList<>();
		orderOverviewVoPage.setRecords(orderOverviewVOList);
		
		orderOverviewPage.getRecords().forEach(orderOverview -> {
			OrderOverviewVO orderOverviewVO = new OrderOverviewVO();
			BeanUtils.copyProperties(orderOverview, orderOverviewVO);
			orderOverviewVOList.add(orderOverviewVO);
		});
		
		orderOverviewVoPage.getRecords().forEach(orderOverviewVO -> {
			Long orderId = orderOverviewVO.getOrderId();
			List<OrderDetail> orderDetailList = orderDetailMapper.selectList(
				Wrappers.<OrderDetail>lambdaQuery().eq(OrderDetail::getOrderId, orderId)
			);
			List<CartGoodsDTO> cartGoodsDTOList = new ArrayList<>(orderDetailList.size());
			orderDetailList.forEach(orderDetail -> {
				CartGoodsDTO cartGoodsDTO = new CartGoodsDTO();
				cartGoodsDTO.setGoodsId(orderDetail.getGoodsId());
				cartGoodsDTO.setGoodsName(orderDetail.getGoodsName());
				cartGoodsDTO.setGoodsPrice(orderDetail.getGoodsPrice());
				cartGoodsDTO.setGoodsQuantity(orderDetail.getGoodsQuantity());
				cartGoodsDTO.setGoodsPicture(orderDetail.getGoodsPicture());
				cartGoodsDTOList.add(cartGoodsDTO);
			});
			orderOverviewVO.setGoodsList(cartGoodsDTOList);
		});
		
		return Response.success(orderOverviewVoPage);
	}
	
}
